// pages/goodsList/index.js

import { request } from "../../request/index";

//Page Object
Page({
  data: {
    tabs:[
      {
      id:0,
      value:"综合",
      isActive:true
    },
    {
      id:1,
      value:"销量",
      isActive:false
    },
    {
      id:2,
      value:"价格",
      isActive:false
    }],
    goodsList:[],
   
  
  },
  QueryParams:{
    query:'',
    cid:'',
    pagenum:1,
    pagesize:10
  },
  totalPages:1,
  //options(Object)
  onLoad: function(options){
    this.QueryParams.cid = options.cid
    this.getGoodsList(options.cid)
  },
  handleTabsItemChange(e){
    const {index} = e.detail;
    let {tabs} = this.data;
    tabs.forEach((v,i) => i===index?v.isActive=true:v.isActive=false);
    console.log(tabs);
    this.setData({
      tabs
    })
  },
  onReachBottom(){
    if(this.QueryParams.pagenum>=this.totalPages){
      wx.showToast({
        title: '触底了',
       
      });
    }else{
      this.QueryParams.pagenum++;
      this.getGoodsList()
    }
  },
  async getGoodsList(cid){
   let res = await request({
      url:'/goods/search',
      method:'get',
      data:this.QueryParams
    })
    let total = res.total;
    this.totalPages = Math.ceil(total/this.QueryParams.pagesize);
    this.setData({
      goodsList:[...this.data.goodsList,...res.goods]
    })
    wx.stopPullDownRefresh()
  },
  onPullDownRefresh(){
    this.setData({
      goodsList:[]
    })
    this.QueryParams.pagenum = 1;
    this.getGoodsList()
  }
});